package org.agony2d.gpu.core.animation {
	import org.agony2d.debug.Logger;
	import org.agony2d.core.agony_internal;
	
	use namespace agony_internal;
	
	/**
		<sheet name = "agony">
			<clip  name="right_stand"  frameRate="7" >   1    </clip>
			<clip  name="right_jump"   frameRate="7" >   2-3  </clip>
			<clip  name="right_run"    frameRate="10">   4-9  </clip>
			<clip  name="left_stand"   frameRate="7" >   1    </clip>
			<clip  name="left_jump"    frameRate="7" >   2-3  </clip>
			<clip  name="left_run"     frameRate="10">   4-9  </clip>
		</sheet>
	*/
public class SheetAA {
  
    public function SheetAA( sheetName:String, totalFrames:int ) {
		m_sheetName = sheetName
		m_totalFrames = totalFrames
		m_clipList = new <FrameClip>[]
		m_clipMap = {}
		//Logger.reportMessage(this, "sheet ( " + sheetName + " )..totalFrames ( " + totalFrames + " )..added.")
    }
	
	agony_internal static function addSheet( sheetName:String, totalFrames:int ) : SheetAA {
		var sheet:SheetAA
		
		if (!m_sheetMap) {
			m_sheetMap = {}
		}
		else if (m_sheetMap[sheetName]) {
			Logger.reportError("SheetAA", "addSheet", "Repeated sheet: ( " + sheetName + " )...!!")
		}
		sheet = m_sheetMap[sheetName] = new SheetAA(sheetName, totalFrames)
		return sheet
	}
	
	agony_internal static function addSheetsByXML( sheetXml:XML ) : void {
		var sheet:SheetAA
		var i:int, l:int, ii:int, ll:int
		var node:XML, node_A:XML
		var sheetName:String
		
		if (!m_sheetMap) {
			m_sheetMap = {}
		}
		l = sheetXml.sheet.length()
		while (i < l) {
			node = sheetXml.sheet[i++]
			sheetName = node.@name
			if (m_sheetMap[sheetName]) {
				Logger.reportError("SheetAA", "addSheet", "Repeated sheet: ( " + sheetName + " )...!!")
			}
			m_sheetMap[sheetName] = sheet = new SheetAA(sheetName, int(node.@totalFrames))
			ii = 0
			ll = node.clip.length()
			while (ii < ll) {
				node_A = node.clip[ii++]
				sheet.addFrameClip(node_A.@name, node_A, Number(node_A.@frameRate))
			}
		}
	}
	
	//agony_internal static function killSheets( ...sheetNames ) : void {
		//
	//}
	
	public function get totalFrames() : int {
		return m_totalFrames
	}
	
	public function get totalClips() : int {
		return m_totalClips
	}
	
	public function addFrameClip( clipName:String, clipData:String, frameRate:Number ) : void {
		var clip:FrameClip
		
		if (m_clipMap[clipName]) {
			Logger.reportError(this, "addFrameClip", "sheet ( " + m_sheetName + " )..repeated clip ( " + clipName + " )...!!")
		}
		clip = m_clipList[m_totalClips++] = m_clipMap[clipName] = new FrameClip(clipData, frameRate)
		//Logger.reportMessage(this, clipName + "...frameRate ( " + frameRate + " ) : " + clipData + " ( " + clip.frames + " )...")
	}
	
	
	agony_internal static var m_sheetMap:Object
	
	agony_internal var m_clipList:Vector.<FrameClip>
	agony_internal var m_clipMap:Object
	agony_internal var m_totalClips:int
	agony_internal var m_totalFrames:int
	agony_internal var m_sheetName:String
	
}
}